﻿using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.Text;

namespace EmbededChannelTest
{
    public class FixedLengthFrameDecoder : ByteToMessageDecoder
    {
        private int _frameLength;
        public FixedLengthFrameDecoder(int frameLength)
        {
            if (frameLength <= 0)
            {
                throw new Exception("不合法的参数。");
            }
            _frameLength = frameLength;
        }
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
        {
            // 解码器实现固定的帧长度
            while (input.ReadableBytes >= _frameLength)
            {
                IByteBuffer buf = input.ReadBytes(_frameLength);
                output.Add(buf);
            }
        }
    }
}
